package edu.mit.media.fv.components
{
	import flash.display.*;
	import flash.events.MouseEvent;
	import flash.filters.*;
	import flash.text.*;
	
	public class FVRadioButton extends Sprite
	{
		private var _label:TextField;
		private var _radio:Sprite;
		private var _width:int;
		private var _height:int;
		private var _foreground:uint;
		private var _bevelUp:BevelFilter = new BevelFilter(4, 45, 0xFFFFFF, 1, 0, 1, 4, 4, 1, 1);
		private var _selected:Boolean = false;
		public var siblings:Array = new Array();
		
		public function FVRadioButton(x:int, y:int, width:int, height:int, text:String){
			super();
			this.x = x;
			this.y = y;
			this._foreground = 0xFFFFFF;
			this._width = width;
			this._height = height;
			
			var format:TextFormat = new TextFormat();
			format.align = TextFormatAlign.CENTER;
			format.bold = true;
			format.font = "Arial";
			format.size = height*.8;
			format.color = _foreground;
			this._label = new TextField();
			this._label.defaultTextFormat = format;
			this._label.type = TextFieldType.DYNAMIC;
			this._label.selectable = false;
			this._label.text = text;
			this._label.width = width-height;
			this._label.height = height;
			this._label.text = text;
			this._label.x = height;
			
			this._radio = new Sprite();
			
			this.addChild(this._radio);
			this.addChild(this._label);
			
			this.addEventListener(MouseEvent.CLICK, radioSelected);
			
			this.reform();
			this._radio.filters = [this._bevelUp];
		}
		public function get text():String{
			return this._label.text;
		}
		public function set selected(selected:Boolean):void{
			this._selected = selected;
			this.reform();
		}
		public function get selected():Boolean{
			return this._selected;
		}
		private function radioSelected(event:MouseEvent):void{
			if(!this.selected){
				this.selected = true;
				for(var i:int = 0; i<this.siblings.length; i++){
					var sibling:FVRadioButton = FVRadioButton(this.siblings[i]);
					if(sibling != this){
						sibling.selected = false;
					}
				}
			}
			
		}
		private function reform():void{
			if(this.selected){
				this._radio.graphics.clear();
				this._radio.graphics.lineStyle(3,0xEEEEEE,1.0,true);
				this._radio.graphics.beginFill(0x000000, 1.0);
				this._radio.graphics.drawCircle(_height/2, _height/2, _height/2-4);
				this._radio.graphics.endFill();
			
			}else{
				this._radio.graphics.clear();
				this._radio.graphics.lineStyle(3,0xEEEEEE,1.0,true);
				this._radio.graphics.beginFill(0xFFFFFF, 0.25);
				this._radio.graphics.drawCircle(_height/2, _height/2, _height/2-4);
				this._radio.graphics.endFill();
			}
			
		}
	}
}